<?php
class ImageResizer
{
 private $regName;
 private $fileName;
 private $contentType;
 private $options;
 private $width;
 private $height;
 private $image;
 function __construct($regName, $fileName, $contentType)
 {
  $this->regName = $regName;
  $registry = Zend_Registry::getInstance();
  $this->options = $registry->get($regName);
  $this->fileName = $fileName;
  $this->contentType = $contentType;
  list($this->width, $this->height) = getimagesize($fileName);
  switch ($contentType)
  {
   case 'image/pjpeg' : $this->image = imagecreatefromjpeg($fileName);
   break;
   case 'image/jpeg' : $this->image = imagecreatefromjpeg($fileName);
   break;
   case 'image/jpg' : $this->image = imagecreatefromjpeg($fileName);
   break;
   case 'image/gif' : $this->image = imagecreatefromgif($fileName);
   break;
   case 'image/png' : $this->image = imagecreatefrompng($fileName);
   break;
   case 'image/x-png' : $this->image = imagecreatefrompng($fileName);
   break;
  }
  
 }
 private function resize($x)
 {
  $this->width > $this->height ? $resized = $this->width : $resized = $this->height; //найдем максимальную сторону
  if($resized < $x) //если максимальная сторона меньше запрошенного размера
  {
   $new_height = $this->height;
   $new_width = $this->width;
   $thumb = $this->image;
  }
  else{
   $new_width = floor($this->width*$x/$resized);
   $new_height = floor($this->height*$x/$resized);
   $thumb = imagecreatetruecolor($new_width, $new_height);
   imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height);
  }
  return $thumb;
 }
 public function resize100()
 {
  $new_width = $this->options->image->width;
  $tmpfname = tempnam('/tmp', 'resized');
  $this->saveImage($this->resize($new_width), $tmpfname);
  return $tmpfname;
 }
 public function resize200()
 {
  $new_width = $this->options->image->width200;
  $tmpfname = tempnam('/tmp', 'resized200');
  $this->saveImage($this->resize($new_width), $tmpfname);
  return $tmpfname;
 }
 public function resize640()
 {
  $new_width = $this->options->image->width640;
  $tmpfname = tempnam('/tmp', 'resized640');
  $this->saveImage($this->resize($new_width), $tmpfname);
  return $tmpfname;
 }
 private function saveImage($image, $fileName)
 {
  switch ($this->contentType)
  {
   case 'image/pjpeg' : imagejpeg($image, $fileName);
   break;
   case 'image/jpeg' : imagejpeg($image, $fileName);
   break;
   case 'image/jpg' : imagejpeg($image, $fileName);
   break;
   case 'image/gif' : imagegif($image, $fileName);
   break;
   case 'image/png' : imagepng($image, $fileName);
   break;
   case 'image/x-png' : imagepng($image, $fileName);
   break;
  }
 }
}